#include "broker.h"

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

struct bcshm_t shm;
struct bcshmseg_t shmseg;

void reader(){
  bcshm_open(&shm);
  
  while(1){
    bcshm_read(FLIGHT_TASK, &shm, &shmseg.flight_task);
    bcshmseg_print(&shmseg);

    usleep(1e6 / 4);
  }
}

void writer(){
  bcshmseg_init(&shmseg);
  bcshm_open(&shm);
  
  for(int i = 1; ; ++i){
    bcshm_write(FLIGHT_TASK, &shm, &shmseg.flight_task);
    shmseg.flight_task.hang_time = i;

    usleep(1e6 / 2);
  }
}


int main(int argc, char* argv[]) {

  if(argc < 2)
    return 0;
  if(!strcmp(argv[1], "i"))
    bcshm_init();
  else if(!strcmp(argv[1], "d"))
    bcshm_delete();
  else if(!strcmp(argv[1], "r"))
    reader();
  else if(!strcmp(argv[1], "w"))
    writer();
  
  bcshm_close(&shm);

  return 0;
}
